package com.small.nacos.common.task.engine;

import com.small.nacos.common.task.NacosTask;
import com.small.nacos.common.task.NacosTaskProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 这里只存储执行器
 *
 * @Author zhoujin
 * @Date 2022/11/12 13:34
 */
public abstract class AbstractNacosTaskExecuteEngine<T extends NacosTask> implements NacosTaskExecuteEngine<T>{

    protected final Logger log;

    private final ConcurrentHashMap<Object, NacosTaskProcessor> taskProcessors = new ConcurrentHashMap<Object, NacosTaskProcessor>();

    public AbstractNacosTaskExecuteEngine(Logger logger) {
        this.log = null != logger ? logger : LoggerFactory.getLogger(AbstractNacosTaskExecuteEngine.class.getName());
    }

    @Override
    public void addProcessor(Object key, NacosTaskProcessor taskProcessor) {
        taskProcessors.putIfAbsent(key, taskProcessor);
    }

    @Override
    public void removeProcessor(Object key) {
        taskProcessors.remove(key);
    }

    @Override
    public NacosTaskProcessor getProcessor(Object key) {
        return taskProcessors.get(key);
    }
}
